package com.woniuxy.springbootpro.utile;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
@ApiModel("统一返回类型")
public class Result {
    @ApiModelProperty("返回的响应的状态码")
    private Integer code;
    @ApiModelProperty("返回的响应的状态")
    private Boolean success;
    @ApiModelProperty("返回的响应的消息")
    private String message;
    @ApiModelProperty("返回的响应的数据")
    private Map<String,Object> data = new HashMap<>();

    public Result() {
    }

    public static Result ok(){
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.MESSAGE_SUCCESS);
        result.setSuccess(true);
        return result;
    }

    public static Result error(){
        Result result = new Result();
        result.setCode(ResultCode.FAILURE);
        result.setMessage(ResultMessage.MESSAGE_FAILURE);
        result.setSuccess(false);
        return result;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result success(boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result data(Map<String,Object> data){
        this.setData(data);
        return this;
    }

    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }
}
